<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.2</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="css/font.css">
    <link rel="stylesheet" href="css/xadmin.css">
    <script src="lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/xadmin.js"></script>
</head>

<body>
<div class="x-nav">
            <span class="layui-breadcrumb">
                <a href="">首页</a>
                <a href="">演示</a>
                <a>
                    <cite>导航元素</cite></a>
            </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form layui-col-space5">
                        <div class="layui-input-inline layui-show-xs-block">
                            <input type="text" name="driverName" placeholder="请输入驾驶员姓名" autocomplete="off"
                                   class="layui-input"></div>
                        <div class="layui-form-item demoTable1">
                            <button class="layui-btn" lay-submit="" lay-filter="search" data-type="searchByName">搜索
                            </button>
                        </div>
                    </form>
                </div>
                <div class="layui-btn-group demoTable">
                    <button class="layui-btn layui-btn-danger" data-type="batchDel">
                        <i class="layui-icon"></i>批量删除
                    </button>
                    <button class="layui-btn" onclick="xadmin.open('添加司机信息','driver-add.html',800,600)">
                        <i class="layui-icon"></i>添加
                    </button>
                </div>
                <div class="layui-card-body ">
                    <table class="layui-table layui-form" id="layListId" lay-filter="layList">
                        <script type="text/html" id="barDemo" >
                            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑
                            </a>
                            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除
                            </a>
                        </script>

                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>layui.use(['laydate', 'form', 'table'],
    function () {
        var laydate = layui.laydate;
        var form = layui.form;
        var table = layui.table;
        laydate.render({
            elem: '#driverDrivingCreateTime' //指定元素
        });
        laydate.render({
            elem: '#driverDrivingExpiredTime' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
            elem: '#end' //指定元素
        });
        /*表格显示数据*/
        table.render({
            elem: '#layListId',
            id: 'layTableId',
            url: '/driver/list'+location.search,
            cellMinWidth: 100,
            cols: [
                [{
                    type: 'checkbox',
                    fixed: 'left',
                }, {
                    title: '司机ID',
                    field: 'driverId',
                    sort: true,
                    offset: 'auto',
                }, {
                    title: '司机姓名',
                    field: 'driverName',
                    offset: 'auto'
                }, {
                    title: '司机联系方式',
                    field: 'driverPhone',
                    sort: true,
                    offset: 'auto'
                }, {
                    title: '持照时间',
                    field: 'driverDrivingCreateTime',
                    sort: true,
                    offset: 'auto'
                }, {
                    title: '驾照到期时间',
                    field: 'driverDrivingExpiredTime',
                    offset: 'auto'
                }, {
                    title: '驾照照片',
                    field: 'driverDrivingPic',
                    offset: 'auto'
                }, {
                    title: '司机身份证正面照',
                    field: 'driverIdcardA',
                    offset: 'auto'
                }, {
                    title: '司机身份证反面照',
                    field: 'driverIdcardB',
                    offset: 'auto'
                },
                    {
                        title: '驾照类型',
                        field: 'driverDrivingType',
                        offset: 'auto'
                    },

                    {
                        title: '司机当前状态',
                        field: 'driverStatus',
                        templet: function (d) {
                            if (d.driverStatus === 1) return '空闲';
                            if (d.driverStatus === 2) return '忙碌中';
                            if (d.driverStatus === 3) return '请假';
                            if (d.driverStatus === 4) return '离职';
                        },
                        offset: 'auto'
                    },{
                    title: '操作',
                    templet: '#opt',
                    offset: 'auto',
                    minWidth: '120',
                    toolbar: '#barDemo'
                }
                ]
            ],
            page: true
        });



        //监听操作,编辑或删除
        table.on('tool(layList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    var id = data.driverId;
                    $.ajax({
                        url: "./driver/delete/"+id,
                        method: 'post',
                        success: function (res) {
                            if (res.code == 200) {
                                layer.alert("删除成功", {
                                        icon: 6
                                    },
                                    function () {
                                        //关闭当前frame
                                        xadmin.close();
                                        // 可以对父窗口进行刷新
                                        xadmin.father_reload();
                                    });
                            } else
                                alert(res.msg);
                        },
                        error: function (res) {
                            alert("服务器繁忙！");
                        }
                    });
                });
            } else if (obj.event === 'edit') {
                xadmin.open('编辑', './driver-edit.html#/id=' + data.driverId, 800, 500);
            }
        });

        var $ = layui.$, active = {
            //批量删除
            batchDel: function () {
                //获取选中的数据
                var arr = [];
                var checkStatus = table.checkStatus('layTableId'),
                    data = checkStatus.data;
                for (var i = 0; i < data.length; i++) {
                    arr.push(data[i].driverId);   // 循环筛选出id
                }

                if (data.length == 0) {
                    parent.layer.msg("请选择要删除的司机", {icon: 2});
                    return;
                } else {
                    layer.confirm('确定要删除吗?', function (index) {
                        $.ajax({
                            url: './driver/deleteBatch',
                            method: 'post',
                            data: "ids=" + arr,
                            success: function (res) {
                                if (res.code == 200) {
                                    layer.alert("删除成功", {
                                            icon: 6
                                        },
                                        function () {
                                            //关闭当前frame
                                            xadmin.close();
                                            // 可以对父窗口进行刷新
                                            xadmin.father_reload();
                                        });
                                } else {
                                    alert(res.msg);
                                }
                            }, error: function (data) {
                                alert("服务器繁忙,请稍后再试！");
                            }
                        });
                        return false;
                    })
                }
            },
        }
        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
        var param = new Object();
        param = getUrlParam();
      /*  $('#start').attr('value', param["start"]);
        $('#end').attr('value', param["end"]);*/
        $('#driverName').attr('value', param["driverName"]);

        function getUrlParam() {
            var url = location.search;      //获取url中"?"符后的字符串包括‘？’ ，window.location.href获取的是url完整的字符串
            var theParam = new Object();
            if (url.indexOf("?") != -1) {    //确保‘？’不是最后一个字符串，即携带的参数不为空
                var str = url.substr(1);    //substr是字符串的用法之一，抽取指定字符的数目，这里抽取？后的所有字符
                strs = str.split("&");      //将获取到的字符串从&分割，输出参数数组，即输出[参数1=xx,参数2=xx,参数3=xx,...]的数组形式
                for (var i = 0; i < strs.length; i++) {     //遍历参数数组
                    theParam[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);     //这里意思是抽取每个参数等号后面的值，unescape是解码的意思
                }
            }
            return theParam; //返回参数值
        }
</script>
</html>